//
//  SN_InputQuestionPwd_Alert.m
//  Shiyun_Newspaper
//
//  Created by zz on 17/8/4.
//  Copyright © 2017年 ShiYun. All rights reserved.
//

#import "SN_InputQuestionPwd_Alert.h"

#import "SN_ForgetAndEditPwd_Controller.h"
#import "SN_Question_Detail_Controller.h"
@interface SN_InputQuestionPwd_Alert() <UITextFieldDelegate>



@end


@implementation SN_InputQuestionPwd_Alert {
    
    UITextField *_field;
}


- (instancetype)initWithSuperView:(UIView *)superView {
    
    if (self = [super init]) {
        
        self.frame = [UIScreen mainScreen].bounds;
        [superView addSubview:self];
        
        self.backgroundColor = RGBACOLOR(1, 1, 1, 0.1);
        
        UIView *alert = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 190)];
        alert.backgroundColor = [UIColor whiteColor];
        alert.center = CGPointMake(self.width / 2, self.height / 2 - 50);
        alert.layer.cornerRadius = 10;
        [self addSubview:alert];
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(cancelAction)];
//        [self addGestureRecognizer:tap];
        
        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(30, 16, 200, 17)];
        titleLabel.font = [UIFont systemFontOfSize:17];
        titleLabel.backgroundColor = [UIColor clearColor];
        titleLabel.textColor = [UIColor colorWithHexString:@"#3b3b3b"];
        titleLabel.text = @"输入解题密码以继续解题";
        [alert addSubview:titleLabel];
        
        UITextField *field = [[UITextField alloc] initWithFrame:CGRectMake(30, CGRectGetMaxY(titleLabel.frame) + 17, 300 - 60, 44)];
        _field = field;
        field.delegate = self;
        field.layer.cornerRadius = 4;
        field.layer.borderWidth = 0.5;
        field.font = [UIFont systemFontOfSize:16];
        field.layer.borderColor = [UIColor colorWithHexString:@"#dedede"].CGColor;
        field.placeholder = @"请输入您的解题密码";
        field.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 9, 9)];
        field.leftViewMode = UITextFieldViewModeAlways;
        field.secureTextEntry = YES;
        field.keyboardType = UIKeyboardTypeNumberPad;
        [field becomeFirstResponder];
        [alert addSubview:field];
        
        UIButton *forgetBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [forgetBtn setTitleColor:[UIColor colorWithHexString:@"#f55f5f"] forState:UIControlStateNormal];
        [forgetBtn setFrame:CGRectMake(300 - 30 - 100, CGRectGetMaxY(field.frame) + 16, 100, 14)];
        forgetBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
        [forgetBtn setTitle:@"忘记解题密码" forState:UIControlStateNormal];
        forgetBtn.titleLabel.font = [UIFont systemFontOfSize:14];
        [forgetBtn addTarget:self action:@selector(forgetPwd:) forControlEvents:UIControlEventTouchUpInside];
        [alert addSubview:forgetBtn];
        
        UIView *lineV = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(forgetBtn.frame) + 21, 300, 0.5)];
        lineV.backgroundColor = [UIColor colorWithHexString:@"#dedede"];
        [alert addSubview:lineV];
        
        UIButton *_cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_cancelButton setTitle:@"取消" forState:UIControlStateNormal];
        [_cancelButton setTitleColor:[UIColor colorWithHexString:@"#3b3b3b"] forState:UIControlStateNormal];
        [_cancelButton addTarget:self action:@selector(cancelAction) forControlEvents:UIControlEventTouchUpInside];
        _cancelButton.titleLabel.font = [UIFont systemFontOfSize:17];
        _cancelButton.frame = CGRectMake(0, CGRectGetMaxY(lineV.frame), (300 - 0.5)/2, 45);
        [alert addSubview:_cancelButton];
        
        UIView *divView = [[UIView alloc] initWithFrame:CGRectMake(CGRectGetMaxX(_cancelButton.frame), CGRectGetMaxY(lineV.frame), 0.5, 45)];
        divView.backgroundColor = [UIColor colorWithHexString:@"#dedede"];
        [alert addSubview:divView];
        
        UIButton *_confirmButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_confirmButton setTitle:@"确定" forState:UIControlStateNormal];
        [_confirmButton setTitleColor:[UIColor colorWithHexString:@"#3b3b3b"] forState:UIControlStateNormal];
        _confirmButton.titleLabel.font = [UIFont systemFontOfSize:17];
        _confirmButton.frame = CGRectMake(CGRectGetMaxX(divView.frame), CGRectGetMaxY(lineV.frame), (300 - 0.5) / 2, 45);
        [_confirmButton addTarget:self action:@selector(confirmAction) forControlEvents:UIControlEventTouchUpInside];
        [alert addSubview:_confirmButton];

    }
    
    return self;
}


- (void)cancelAction {
    
    if (self.cancelBlock) {
        
        self.cancelBlock();
    }
    
    [self removeFromSuperview];
}

- (void)confirmAction {
    
    if (_field.text.length != 4) {
        
        [Utils alertViewWithMessage:@"请输入四位解题密码"];
        return;
    }
    
    NSString *pwdStr = [SYAccountTool ShareAccount].account.szbpwd;
    
    if (![_field.text isEqualToString:pwdStr]) {
        
        [Utils alertViewWithMessage:@"密码输入错误,请重新输入"];
        return;
    }
    
    if (_confirmBlock) {
        
        _confirmBlock();
        [self removeFromSuperview];
    }
}

- (void)forgetPwd:(UIButton*)sender {
    
    [self endEditing:YES];
    
    SN_ForgetAndEditPwd_Controller *vc = [[SN_ForgetAndEditPwd_Controller alloc] init];
    vc.type = 1;
    vc.confirmBlock = ^(int type) {
        
        if (type == 1) {
            
            _field.text = @"";
        }
    };
    
    SN_Question_Detail_Controller *detailVC = (SN_Question_Detail_Controller *)sender.nextResponder.nextResponder.nextResponder.nextResponder;
    [detailVC.navigationController pushViewController:vc animated:YES];
}


#pragma mark -UITextFieldDelegate
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    
    if (string.length != 0 && textField.text.length >= 4) {
        return NO;
    }
    
    return YES;
}

@end
